package com.bupt.inklue.service;

import com.bupt.inklue.dto.HanZiDTO;
import com.bupt.inklue.pojo.HanZi;
import com.bupt.inklue.pojo.Work;
import com.bupt.inklue.repository.HanZiRepository;
import com.bupt.inklue.repository.WorkRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class HanZiService {

    @Autowired
    private HanZiRepository hanZiRepository;
    @Autowired
    private WorkRepository workRepository;

    public HanZiDTO getByCode(String code) {
        HanZi hanZi = hanZiRepository.findByCode(code).orElseThrow();
        Work work = workRepository.findByCode(hanZi.getWorkCode()).orElseThrow();
        return new HanZiDTO(hanZi, work);
    }

    public List<HanZiDTO> getAll() {
        return ((List<HanZi>) hanZiRepository.findAll()).stream()
                .map(hanZi -> {
                    Work work = workRepository.findByCode(hanZi.getWorkCode()).orElseThrow();
                    return new HanZiDTO(hanZi, work);
                })
                .collect(Collectors.toList());
    }
}
